package com.app.newiptv.promote;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;


import com.app.newiptv.utils.APPLog;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UpgradeUtil {
    public interface DownloadListener {
        void onLoading(int progress, int contentLen);

        void onLoaded(File apk);

        void onFailed();
    }

    public static File upgrade(String addr, String loadDir, DownloadListener listener) throws Exception {
        URL url = new URL(addr);
        APPLog.printInfo(addr+"下载地址："+url);
        //String path = url.getPath();
        //int index = path.lastIndexOf("/") + 1;
        //String fileName = path.substring(index);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(30 * 1000);
        InputStream in = conn.getInputStream();
        File loadFileDir = new File(loadDir);
        if (!loadFileDir.exists()) {
            loadFileDir.mkdirs();
        }
        File downloadFile = new File(loadFileDir, "an_launhcer.apk");
        FileOutputStream out = new FileOutputStream(downloadFile);
        byte[] buf = new byte[1024];
        int progress = 0;
        int contentLen = conn.getContentLength();
        int len = in.read(buf);
        while (len != -1) {
            progress += len;
            out.write(buf, 0, len);
            if (listener != null) {
                listener.onLoading(progress, contentLen);
            }
            len = in.read(buf);
        }
        if (listener != null) {
            if (progress == 0) {
                listener.onFailed();
            } else {
                listener.onLoaded(downloadFile);
            }
        }
        in.close();
        out.close();
        return downloadFile;
    }

    public static void install(Context context, File apk) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.parse("file://" + apk.getAbsolutePath()), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    public static int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            String packageName = context.getPackageName();
            versionCode = context.getPackageManager().getPackageInfo(packageName, 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    public static String getVersionName(Context context) {
        String versionName = "";
        try {
            String packageName = context.getPackageName();
            versionName = context.getPackageManager().getPackageInfo(packageName, 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }
}
